import axios from '@/api'
import MD5 from 'js-md5'
import { IArea } from './area'

export type IYear = Record<number | string, Array<IProject>>

export interface IUserLoginAdmin {
  pow_gp_id: string;
  pow_gp_nam: string;
  s_a_id: string;
  s_a_nam: string;
  s_a_nam_all: string;
  task_ids: string;
  work_place: string;
  task_info: Array<IProject>;
  year_info: IYear;
  title1?: string;
  title2?: string;
  title3?: string;
}
export interface IUserLogin {
  request: {
    usr_tel?: string;
    usr_act?: string;
    usr_sfz?: string;
    usr_psw: string;
  },
  response: {
    token: string;
    token2: string;
    usr_id: string;
    usr_nickname: string;
    usr_tel: string;
    usr_sfz: string;
    usr_act: string;
    admin_info: IUserLoginAdmin
  }
}

export interface IAdmin {
  usr_id: string;
  s_a_id: string;
  work_place: string;
  pow_gp_id: string;
  task_ids: string;
  statu: string;
  readme: string;
}

export interface IGetAdminList {
  request: Partial<IAdmin> & { next?: number },
  response: Array<IAdmin>
}

export interface IProject {
  task_id: string;
  task_nam: string;
  task_area_tab: string;
  task_usr_tab: string;
  area_info: Array<IArea>;
}
export interface IGetProject {
  response: Array<IProject>
}

class UserServices {
  constructor() { }

  /**
   * 用户登录
   *
   * @returns
   * @memberof UserServices
   */
  login(data: IUserLogin['request']) {
    return axios.post<unknown, Api.ResponseOptions<IUserLogin['response']>>('/btpub/user/pswd-login', data)
  }

  /**
   * 获取管理员列表
   *
   * @param {IGetAdminList['request']} data
   * @returns
   * @memberof UserServices
   */
  getAdminList(data: IGetAdminList['request']) {
    return axios.post<unknown, Api.ResponseOptions<IGetAdminList['response']>>('/btsys/admin/get-admin-list', data)
  }

  /**
   * 编辑/添加管理员
   *
   * @param {IAdmin} data
   * @returns
   * @memberof UserServices
   */
  updateAdmin(data: IAdmin) {
    return axios.post<unknown, Api.ResponseOptions<IGetAdminList['response']>>('/btsys/admin/edit-admin', data)
  }

  /**
   * 删除管理员
   *
   * @param {string} id
   * @returns
   * @memberof UserServices
   */
  deleteAdmin(data: Pick<IAdmin, 'usr_id'>) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/admin/del-admin', data)
  }
  // getProjectsList() {
  //   return axios.post<unknown, Api.ResponseOptions<IGetProject['response']>>('/btsys/admin/get-power-group-list')
  // }

  /**
   * 设置管理员密码
   *
   * @param {Pick<IAdmin, 'usr_id'>} data
   * @returns
   * @memberof UserServices
   */
  updateAdminPassword(data: Pick<IAdmin, 'usr_id'> & { pswd: string }) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/admin/set-pswd', data)
  }

  /**
   * 获取项目列表
   *
   * @returns
   * @memberof UserServices
   */
  getProjectsList(data: { s_a_id?: string }) {
    return axios.post<unknown, Api.ResponseOptions<IGetProject['response']>>('/btsys/task/get-task-list', data)
  }

  /**
   * 获取管理员可配置项目列表
   *
   * @returns
   * @memberof UserServices
   */
  getAdminProjectsList(data: { s_a_id?: string }) {
    return axios.post<unknown, Api.ResponseOptions<IGetProject['response']>>('/btsys/admin/get-task-list', data)
  }

  /**
   * 更新项目
   *
   * @returns
   * @memberof UserServices
   */
  updateProject(data: IProject) {
    return axios.post<unknown, Api.ResponseOptions>('/btsys/task/edit-task', data)
  }

  /**
   * 重置密码
   *
   * @returns
   * @memberof UserServices
   */
  resetPassword(data: { usr_psw_old: string, usr_psw_new: string }) {
    return axios.post<unknown, Api.ResponseOptions>('/btuser/user/reset-pswd', {
      usr_psw_old: MD5(data.usr_psw_old),
      usr_psw_new: MD5(data.usr_psw_new)
    })
  }

  /**
   * 更新用户自己信息
   *
   * @param {{
   *     usr_tel: string;
   *     usr_sfz: string;
   *     usr_nickname: string;
   *   }} data
   * @returns
   * @memberof UserServices
   */
  update(data: {
    usr_tel: string;
    usr_sfz: string;
    usr_nickname: string;
  }) {
    return axios.post<unknown, Api.ResponseOptions>('/btuser/user/edit', data)
  }

}

export const userServices = new UserServices()

export default UserServices